#!/usr/bin/env python
# encoding: utf-8
"""
untitled.py

Created by Roland Becker on 2007-04-08.
Copyright (c) 2007 CONCHA. All rights reserved.
"""

import sys, os, shutil
import random
from Tkinter import *

class Application(Frame): 
  def __init__(self, master=None): 
    Frame.__init__(self, master)
    self.grid()
    self.op1 = StringVar() 
    self.op2 = StringVar()
    self.res = StringVar()
    self.op1.set("12")
    self.op2.set("12")
    self.faux = []
    self.repete = 0
    self.nfaux = StringVar()
    self.nfaux.set("0")
    self.createWidgets() 

  def __repete(self):
    self.repete = 1
     
  def __ControleResultat(self, event):
    x =  int(self.op1.get())
    y =  int(self.op2.get())
    try: 
      z =  int(self.res.get())
    except :
      self.FrameResultat.Resultat.configure(text="mauvaise entrée", background="yellow")
      self.res.set("")
      return
    
    self.res.set("")
    if x*y == z :
      newtext ='bonne réponse !'
      newcolor ='green'
    else :
      newtext = 'non, la solution est: '+str(x)+'*'+str(y)+'='+str(x*y)
      newcolor ='yellow'
      self.faux.append((x,y))
      self.nfaux.set(str(int(self.nfaux.get())+1))
    self.FrameResultat.Resultat.configure(text=newtext, background=newcolor)
    if self.repete and len(self.faux):
      self.nfaux.set(str(int(self.nfaux.get())-1))
      t = self.faux.pop(random.randint(0,len(self.faux)-1))
      x = t[0]
      y = t[1]
    else :
      if self.repete :
          self.FrameResultat.Resultat.configure(text="plus rien à répéter ! Bravo !", background="magenta")
          self.repete = 0
      x = random.randint(1,12)
      y = random.randint(1,12)
    self.op1.set(str(x))
    self.op2.set(str(y))
    

  def createWidgets(self):
    top=self.winfo_toplevel()
    top.rowconfigure(0, weight=1) 
    top.columnconfigure(0, weight=1) 
    self.rowconfigure(0, weight=1) 
    self.columnconfigure(0, weight=1)
     
    self.FrameResultat = Frame(self, border=20)
    self.FrameResultat.grid(column=0,rowspan=5, sticky=N+S+E+W)
    self.FrameResultat.Resultat = Label (self.FrameResultat, font=("Times",24), text="pas de resultat", border=10, width=60) 
    self.FrameResultat.Resultat.grid(column=0, sticky=N+S+E+W)
    self.FrameCalcul = Frame(self, border=20)
    self.FrameCalcul.grid(column=0,rowspan=5, sticky=N+S+E+W)
    self.FrameCommand = Frame(self, border=20, width=60)
    self.FrameCommand.grid(column=0, sticky=N+S+E+W)
    self.FrameCommand.repeteButton = Button (self.FrameCommand, text="Répéter", command=self.__repete )
    self.FrameCommand.repeteButton.grid(column=2, row=0)
    self.FrameCommand.finButton = Button (self.FrameCommand, text="Fin", command=self.quit )
    self.FrameCommand.finButton.grid(column=3, row=0)
    self.FrameCommand.Label0 = Label (self.FrameCommand, font=("Times",18), text="", width=20, border=4, relief="flat") 
    self.FrameCommand.Label1 = Label (self.FrameCommand, font=("Times",18), text="no. erreurs: ", width=10, border=4, relief="groove") 
    self.FrameCommand.Label2 = Label (self.FrameCommand, font=("Times",18), textvariable=self.nfaux, width=3, border=4, relief="groove") 
    self.FrameCommand.Label0.grid(padx=4,column=4,row=0)
    self.FrameCommand.Label1.grid(padx=4,column=5,row=0)
    self.FrameCommand.Label2.grid(padx=4,column=6,row=0)
    self.FrameCalcul.Label1 = Label (self.FrameCalcul, font=("Times",32), textvariable=self.op1, width=2, border=4, relief="sunken") 
    self.FrameCalcul.Label2 = Label (self.FrameCalcul, font=("Times",32), text=" X ", width=3)            
    self.FrameCalcul.Label3 = Label (self.FrameCalcul, font=("Times",32), textvariable=self.op2, width=2, border=4, relief="sunken") 
    self.FrameCalcul.Label4 = Label (self.FrameCalcul, font=("Times",32), text=" = ", width=3)
    self.FrameCalcul.Entry1 = Entry (self.FrameCalcul, font=("Times",32), textvariable=self.res, width=3, border=4, relief="sunken") 
    self.FrameCalcul.Label1.grid(column=0, row=0)
    self.FrameCalcul.Label2.grid(column=1, row=0)
    self.FrameCalcul.Label3.grid(column=2, row=0)
    self.FrameCalcul.Label4.grid(column=3, row=0)
    self.FrameCalcul.Entry1.grid(column=4, row=0) 
    self.FrameCalcul.Entry1.bind("<Return>", self.__ControleResultat) 


app = Application() 
app.master.title("Pour Clara et Dalia") 
app.mainloop() 
